package com.platform.wx.mp.config;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Maps;
import com.platform.entity.MpMsgEntity;
import com.platform.service.MpMsgService;
import com.platform.wx.miniapp.handler.MaLogHandler;
import com.platform.wx.miniapp.handler.MaPicHandler;
import com.platform.wx.miniapp.handler.MaQrcodeHandler;
import com.platform.wx.miniapp.handler.MaTemplateMsgHandler;
import com.platform.wx.miniapp.handler.MaTextHandler;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;

@Component
public class WxMaConfiguration {
    private static Map<Integer, WxMaMessageRouter> routers = Maps.newHashMap();
    private static Map<Integer, WxMaService> maServices = Maps.newHashMap();

    @Autowired
    private MpMsgService mpMsgService;
    @Autowired
    private MaTemplateMsgHandler maTemplateMsgHandler;
    @Autowired
    private MaTextHandler maTextHandler;
    @Autowired
    private MaPicHandler maPicHandler;
    @Autowired
    private MaQrcodeHandler maQrcodeHandler;
    @Autowired
    private MaLogHandler maLogHandler;
    
    
    public WxMaService getMaService(Integer appId) {
        WxMaService wxService = maServices.get(appId);
        if (wxService == null) {
    		MpMsgEntity mpMsg = mpMsgService.queryObject(appId);
    		wxService = putWxmaService(mpMsg);
        }

        return wxService;
    }
    
	@PostConstruct
    public void init() {
    	Map<String, Object> params = Maps.newHashMap();
    	params.put("type", 2);
    	List<MpMsgEntity> mpMsgs = mpMsgService.queryList(params);
    	for(MpMsgEntity mpMsg:mpMsgs) {
    		putWxmaService(mpMsg);
        }
    }    
    
    public WxMaService putWxmaService(MpMsgEntity mpMsg) {
    	WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl ();
        config.setAppid(mpMsg.getAppId());
        config.setSecret(mpMsg.getMpSecret());
        config.setToken(mpMsg.getToken());
        config.setAesKey(mpMsg.getAesKey());
        config.setMsgDataFormat("JSON");

        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        routers.put(mpMsg.getId(), this.newRouter(service));
        maServices.put(mpMsg.getId(), service);
        return service;
    }
      
    private WxMaMessageRouter newRouter(WxMaService service) {
        final WxMaMessageRouter router = new WxMaMessageRouter(service);
        router
            .rule().handler(maLogHandler).next()
            .rule().async(false).content("模板").handler(maTemplateMsgHandler).end()
            .rule().async(false).content("文本").handler(maTextHandler).end()
            .rule().async(false).content("图片").handler(maPicHandler).end()
            .rule().async(false).content("二维码").handler(maQrcodeHandler).end();
        return router;
    }
    
}
